library ieee;
	use ieee.std_logic_1164.all;
	use ieee.std_logic_unsigned.all;
	use ieee.numeric_std.all;

library std; 
	use std.textio.all;
 
library work;
	use work.router_pack.all;
	use work.env_pack.all;

-------------------------------------------------------------------------
    
entity loger is

generic (
		log_file:       string  := "simulation.log"
);

port(    
    -- General Signals: --
    RESET           		: in std_logic;
    CLK             		: in std_logic;
    CYCLE_COUNTER   	 : in std_logic_vector(e_cycle_counter_width_c-1 downto 0);
    MODULE_ID		      : in std_logic_vector(2*e_coord_position_width_c-1 downto 0);
    LOGER_TYPE		      : in std_logic;
    
    -- Packet Info: --
    VALID		      : in std_logic;
    SL		      : in std_logic_vector(msl_ind_width-1 downto 0);
    PACKET_SIZE	      : in std_logic_vector(e_packet_size_width_c-1 downto 0);
    PACKET_ID	        : in std_logic_vector(e_packet_id_width_c-1 downto 0)
);

end loger;

-------------------------------------------------------------------------

architecture loger_arch of loger is
	file l_file: TEXT open write_mode is log_file;
begin    

--	process (RESET)
--	variable line_str: line;
--	begin
--		if (RESET'event) and (RESET='0') then
--			write(line_str,string'("Time Stamp [Base Cycles];"));
--			write(line_str,string'("ModuleID;"));
--			write(line_str,string'("Sender ModuleID (for Receivers);"));
--			write(line_str,string'("PacketID;"));
--			write(line_str,string'("Sent/Received;"));
--			write(line_str,string'("Packet Size [flits];"));
--			write(line_str,string'("Service Level;"));
--			writeline(l_file, line_str);
--		end if;
--	end process;

	process (RESET,CLK)
	variable line_str: line;
	begin
		if (CLK'event and CLK='1') then
			if (VALID='1') then
			
				-- Time Stamp
				write(line_str,(TO_INTEGER(unsigned(CYCLE_COUNTER)) * e_base_clock_phase_ps_c * 2));
				write(line_str,string'("; "));
				
				-- Module ID
				write(line_str,TO_INTEGER(unsigned(MODULE_ID(e_coord_position_width_c*2-1 downto e_coord_position_width_c)))); 	
				write(line_str,string'(", "));				
				write(line_str,TO_INTEGER(unsigned(MODULE_ID(e_coord_position_width_c-1 downto 0))));
				write(line_str,string'("; "));
				
				-- Sender ModuleID (for Receiver only)
				if (LOGER_TYPE = loger_type_collector) then
					write(line_str,TO_INTEGER(unsigned(PACKET_ID(e_packet_counter_width_c+2*e_coord_position_width_c-1 downto e_packet_counter_width_c+e_coord_position_width_c))));
					write(line_str,string'(", "));
					write(line_str,TO_INTEGER(unsigned(PACKET_ID(e_packet_counter_width_c+e_coord_position_width_c-1 downto e_packet_counter_width_c))));
				end if;
				write(line_str,string'("; "));
				
				-- PacketID
				write(line_str,TO_INTEGER(unsigned(PACKET_ID(e_packet_counter_width_c-1 downto 0))));
				write(line_str,string'("; "));
				
				-- Sent/Received
				if (LOGER_TYPE = loger_type_injector) then
					write(line_str,string'("Sent"));
				else
					write(line_str,string'("Received"));
				end if;
				write(line_str,string'("; "));
				
				-- Packet Size
				write(line_str,TO_INTEGER(unsigned(PACKET_SIZE)));
				write(line_str,string'("; "));
				
				-- Service Level
				write(line_str,TO_INTEGER(unsigned(SL)));
				write(line_str,string'("; "));
				
				writeline(l_file, line_str);
				
			end if;
		end if;
	end process;
end loger_arch;
